package com.jintoufs.web.action.travelagency;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.images.Images;
import com.jintoufs.domain.travelagency.Poi;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.image.ImageService;
import com.jintoufs.service.travelagency.PoiService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("poi")
public class PoiAction extends SupportAction {

    @Resource
    private PoiService poiService;
    @Resource
    private ImageService imageService;

    @RequestMapping("list")
    public String list(String agencyId, Model model) {
        model.addAttribute("agencyId", agencyId);
        return "/poi/list";
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "编辑POI")
    public void oper(HttpServletResponse response, String _id, String oper, Poi poi, String logo) {
        AjaxMsg ajaxMsg = new AjaxMsg();

        try {
            if (oper.equals("del") && _id != null) {
                poiService.delete(_id);

            } else {

                switch (oper) {
                    case "edit":
                        if (!ValidUtil.isEmpty(poi.getAgencyId())) {
                            poiService.update(poi);
                            ajaxMsg.setCode("200");
                            ajaxMsg.setMessage("更新成功");
                        } else {
                            ajaxMsg.setCode("500");
                            ajaxMsg.setMessage("更新失败");
                        }
                        break;
                    case "add":
                        String id = getUUID();
                        poi.setId(id);
                        if (!ValidUtil.isEmpty(poi.getAgencyId())) {
                            poiService.insert(poi);
                            if (logo != null) {
                                Images images = new Images();
                                images.setId(getUUID());
                                images.setPath(logo);
                                images.setCreateTime(new Date());
                                images.setValueId(id);
                                images.setCover("1");
                                imageService.insert(images);
                            }
                            ajaxMsg.setCode("200");
                            ajaxMsg.setMessage("添加成功");
                        } else {
                            ajaxMsg.setCode("500");
                            ajaxMsg.setMessage("添加失败");
                        }
                        break;
                    default:
                        ajaxMsg.setCode("500");
                        ajaxMsg.setMessage("操作代码错误！系统只提供增加/修改/删除功能。");
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode("500");
            if (e instanceof DuplicateKeyException) {
//                ajaxMsg.setMessage("此酒店名已存在！");
//            } else {
                ajaxMsg.setMessage("程序出错，请联系管理员！");
            }
        }
        writeInfo(response, JSON.toJSONString(ajaxMsg));
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除POI")
    public void delete(HttpServletResponse response, String[] id) {
        for (String _id : id) {
            poiService.delete(_id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

    @RequestMapping("/query")
    @ResponseBody
    @SystemControllerLog(description = "获取POI列表")
    public JqgridPage query(String agencyId, int page, int rows, String sidx,
                            String sord, String filters, @CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        QueryRule role = new QueryRule();
        if (!ValidUtil.isEmpty(agencyId)) {
            role = new QueryRule();
            role.setOp("eq");
            role.setField("agency_id");
            role.setData(agencyId);
            filters_.getRules().add(role);
        }

        if(customer!=null){
            role = new QueryRule();
            role.setOp("eq");
            role.setField("c.id");
            role.setData(customer.getId());
            filters_.getRules().add(role);

        }
        List<Map> list = poiService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }


    @RequestMapping("/input")
    @SystemControllerLog(description = "查看POI详细信息")
    public String input(String id, Model model, String isEdit, String agencyId) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        if (!ValidUtil.isEmpty(id)) {
            param.put("valueId", id);
            model.addAttribute("entity", poiService.getByPrimaryKey(id));
        }
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("agencyId", agencyId);
        model.addAttribute("title", "POI信息");
        return "poi/input";
    }


}
